﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Balloony
{
	static class Program
	{
		private static int timeToLive = 5000;
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		/// 
		[STAThread]
		static void Main(string[] Args)
		{
			int size = Args.Length;
			if (size < 2)
			{
				return;
			}

			string title = Args[0];
			string body = Args[1];
			
			if (size == 3) {
				timeToLive =  Int32.Parse(Args[2]);
			}

			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			Form1 f = new Form1();
			using (NotifyIcon icon = new NotifyIcon())
			{
				icon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath);
				icon.ContextMenu = new ContextMenu(new MenuItem[] {
                new MenuItem("Exit", (s, e) => { Application.Exit(); }),
            });
				icon.Visible = true;
				showBalloon(f, icon, title, body);
				Application.Run();
				icon.Visible = false;

			}
		}

		private static void showBalloon(Form1 form, NotifyIcon icon, string title, string body)
		{
			icon.Visible = true;

			if (title != null)
			{
				icon.BalloonTipTitle = title;
			}

			if (body != null)
			{
				icon.BalloonTipText = body;
			}

			icon.ShowBalloonTip(timeToLive);

			StartAsyncTimedWork();
		}

		private static async Task delayedWork()
		{
			await Task.Delay(timeToLive);
			Application.Exit();
		}

		private static void StartAsyncTimedWork()
		{
			Task ignoredAwaitableResult = delayedWork();
		}
	}
}
